// HardCodeImageTool
//
// 工具代码 用于Objc生成硬编码图像
// Swift5.3
//
// Created by JzProl.m.Qiezi on 2017/2/10.


import UIKit

// HardCodeImage
struct HardCodeImage{
    var length:Int
    var bytes:[UInt8]?
    
    init(bytes:[UInt8], length:Int){
        self.length = length
        self.bytes = bytes
    }
    
    init(data:Data?) {
        self.length = (data?.count)!
        self.bytes = [UInt8](repeating: 0, count: self.length)
        
        for (index, item) in (data?.enumerated())! {
            self.bytes?[index] = item&0xff
        }
    }
    
    func toUIImage() -> UIImage? {
        let image = UIImage(data: Data(_: self.bytes!))
        return image
    }
    
}

//扩展数组
extension Collection where Iterator.Element == UInt8{
    
    func toString()->String {
        
        var string : String = "["
        for (index,item) in self.enumerated(){
            let xHexStr = String(format:"%X",item&0xff)
            if xHexStr.lengthOfBytes(using: String.Encoding.utf8) == 1 {
                let format = index == 0 ? "0x0%@" : ",0x0%@"
                let hexStr = String(format:format,xHexStr)
                string.append(hexStr)
            }
            else{
                let format = index == 0 ? "0x%@" : ",0x%@"
                let hexStr = String(format:format,xHexStr)
                string.append(hexStr)
            }
        }
        string.append("]")
        return string
    }
}


// iOS
let image = UIImage(named:"close.png")

let data =  image!.pngData()
print("image data length :\(data!.count)")

var hardCodeImage = HardCodeImage(data: data)
print("image data bytes :\n\(String(describing: hardCodeImage.bytes?.toString()))")

let hardcodeBytes :[UInt8] = [0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0x00,0x00,0x00,0x0D,0x49,0x48,0x44,0x52,0x00,0x00,0x00,0x23,0x00,0x00,0x00,0x23,0x08,0x06,0x00,0x00,0x00,0x1E,0xD9,0xB3,0x59,0x00,0x00,0x00,0x01,0x73,0x52,0x47,0x42,0x00,0xAE,0xCE,0x1C,0xE9,0x00,0x00,0x00,0x1C,0x69,0x44,0x4F,0x54,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x12,0x00,0x00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0x7E,0x5D,0xA6,0x99,0x0B,0x49,0x42,0x69,0xCE,0xB6,0xED,0xA6,0x28,0xEA,0x79,0x9E,0xED,0xBA,0xAE,0x25,0xC6,0x64,0x53,0x4B,0xD1,0xA9,0xD0,0x8C,0x96,0x52,0x24,0x35,0x16,0x9F,0x82,0x68,0xA4,0xDB,0xED,0x3A,0xE1,0xB4,0x56,0xE0,0xD6,0x68,0x46,0x2A,0x96,0x25,0x1E,0x9F,0x06,0x0D,0x61,0x4A,0xEF,0x6B,0x18,0x09,0x3C,0xFC,0x01,0x00,0x00,0xFF,0xFF,0xE9,0xD2,0x3A,0xF6,0x00,0x00,0x01,0xE9,0x49,0x44,0x41,0x54,0xB5,0x95,0xC1,0x4A,0xC3,0x40,0x10,0x86,0x6B,0x3D,0x79,0xF3,0xAA,0x86,0x1E,0xFA,0x08,0x82,0xFA,0x0E,0x5E,0xF5,0x52,0x41,0x08,0x0A,0xD5,0x67,0x92,0x0A,0x3D,0x08,0x22,0x46,0x1A,0x42,0x03,0xA5,0x84,0x60,0x54,0xC4,0xB4,0xB4,0xA4,0x2D,0xF4,0x6D,0xBA,0x3A,0x7F,0x68,0xC2,0x64,0xB3,0x89,0x1B,0x68,0x0B,0x43,0x76,0x67,0x67,0xE6,0xFF,0x76,0xB2,0xD9,0xD6,0x0C,0xC3,0x30,0xC9,0xDA,0x55,0xAD,0xD1,0x68,0xDC,0x85,0x61,0xE8,0xFD,0x4A,0xBF,0x20,0x08,0xDC,0xAA,0xB5,0xD6,0xF1,0x66,0x8D,0x06,0x57,0x55,0x93,0x01,0x32,0x1E,0x8F,0x7D,0x89,0x23,0x9D,0xFA,0xBE,0xEF,0x54,0xAD,0x49,0xF1,0x2D,0xC0,0x9C,0x57,0x49,0x04,0xC8,0x64,0x32,0x79,0x4F,0x95,0x69,0x80,0x6E,0xC8,0x5D,0xF2,0x3C,0xCF,0xAE,0x52,0x17,0x1C,0x80,0x39,0xD1,0x4D,0x02,0x48,0x14,0x45,0x1F,0x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0xDB,0x04,0x42,0x7D,0xE8,0x64,0xF4,0x33,0x13,0xB6,0x78,0x40,0x63,0x93,0x6C,0x1B,0x40,0xA8,0x8B,0xFA,0x39,0xED,0x9C,0x83,0x05,0xA1,0x85,0x97,0x64,0x9B,0x04,0xC2,0x99,0xCC,0xBC,0x1A,0xA6,0xA7,0x7C,0x4D,0x1C,0x10,0x87,0x0B,0xA7,0x5D,0xF9,0xD9,0x93,0x5F,0x17,0x14,0xF9,0xA8,0x93,0x1E,0x56,0x1A,0x73,0x9D,0x78,0x9C,0x73,0xA8,0x82,0xC8,0x87,0x7B,0x00,0x17,0x53,0xE9,0x4D,0x4D,0xEB,0x32,0x1C,0xE2,0x91,0x17,0xDF,0x23,0xF4,0x2C,0xD5,0x2B,0x5D,0x54,0x24,0xEF,0x90,0xEF,0x88,0x0C,0x7F,0xAE,0xF8,0xB7,0x6F,0x91,0x99,0x64,0x80,0xC0,0x13,0x73,0xF8,0xB1,0x8E,0x38,0xC4,0x6B,0x6B,0xFC,0x01,0x33,0xDE,0xF8,0x0A,0x01,0x35,0xA8,0xF3,0x00,0x00,0x00,0x00,0x49,0x45,0x4E,0x44,0xAE,0x42,0x60,0x82]

var hardCodeImage2 = HardCodeImage(bytes: hardcodeBytes,length: 1108)
let vImage = hardCodeImage2.toUIImage()



